package topo.sourcecode;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.Caret;

public class SourceCodeTextArea extends JTextArea implements ActionListener {
    private JPopupMenu consoleMenu;
    private JMenuItem copyItem;
    private JMenuItem selectAllItem;
    Font font = new Font("Courier", 0, 14);

    public SourceCodeTextArea() throws IOException {
        initCoreCodeArea();
    }

    private void initCoreCodeArea() {
        this.consoleMenu = new JPopupMenu();
        this.copyItem = new JMenuItem("复制");
        this.selectAllItem = new JMenuItem("全部选中");

        this.consoleMenu.add(this.copyItem);
        this.consoleMenu.addSeparator();
        this.consoleMenu.add(this.selectAllItem);

        this.copyItem.addActionListener(this);
        this.selectAllItem.addActionListener(this);

        add(this.consoleMenu);
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.getButton() == 3) {
                    SourceCodeTextArea.this.copyItem.setEnabled(SourceCodeTextArea.this.isCanCopy());
                    SourceCodeTextArea.this.consoleMenu.show(SourceCodeTextArea.this, e.getX(), e.getY());
                }
            }

            public void mouseEntered(MouseEvent mouseEvent) {
                SourceCodeTextArea.this.setCursor(new Cursor(2));
            }
        });
        getCaret().addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                SourceCodeTextArea.this.getCaret().setVisible(true);
            }
        });
        setEditable(false);
        setFont(this.font);
        setCaretColor(Color.RED);

        setTabSize(4);
    }

    public boolean isCanCopy() {
        boolean b = false;
        int start = getSelectionStart();
        int end = getSelectionEnd();
        if (start != end)
            b = true;
        return b;
    }

    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        if (str.equals(this.copyItem.getText())) {
            copy();
        } else if (str.equals(this.selectAllItem.getText())) {
            selectAll();
        }
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("CoreCodeArea测试");
        SourceCodeTextArea consoleTextArea = null;
        try {
            consoleTextArea = new SourceCodeTextArea();
        } catch (IOException e) {
            e.printStackTrace();
        }

        f.getContentPane().add(new JScrollPane(consoleTextArea), "Center");
        f.setBounds(50, 50, 300, 300);
        f.setVisible(true);

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}